package cn.tedu.egarticle01.base.exception;

import cn.tedu.egarticle01.base.response.JsonResult;
import cn.tedu.egarticle01.base.response.StatusCode;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        return JsonResult.fail(e.getFieldError().getDefaultMessage());
    }
@ExceptionHandler
    public JsonResult doHandleConstraintViolationException (ConstraintViolationException e){
        return new JsonResult(StatusCode.VALIDATED_ERROR,e.getMessage().split(": ")[1]);
    }
}
